<?php
namespace app\model;
use app\BaseModel;
use app\console\model\Config;
use app\model\Member;
use think\model\concern\SoftDelete;

class MemberBrokerage extends BaseModel
{

    //use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort','status'];

    public function searchTitleAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('order_number', 'like', '%' . $value . '%');
        }
    }
    public function searchTypeAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('type', '=', $value);
        }
    }


    public function searchMemberIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('member_id', '=', $value);
        }
    }


    public function setMemberIdAttr($value, $data)
    {
        //当前字段的修改器
        $member = Member::where('id', $value)->find();
        if(!empty($member)) {
            $this->set('member_mobile', $member['mobile']);
            $this->set('member_level_id', $member['level_id']);
            $this->set('member_nickname', $member['nickname']);
            $this->set('member_name', $member['name']);
        }
        return $value;
    }

    protected function setFromIdAttr($value, $data)
    {
        //当前字段的修改器
        $member = Member::where('id', $value)->find();
        if(!empty($member)){
            $this->set('from_mobile', $member['mobile']);
            $this->set('from_nickname', $member['nickname']);
            $this->set('from_name', $member['name']);
            $this->set('from_level_id', $member['level_id']);
        }
        return $value;
    }

    public static function saveData($member_id,$from_id=0, $type, $mark = '+', $brokerage, $price, $order_number, $title = '')
    {


            if ($mark == '-') {
                $member_brokerage = Member::where('id', $member_id)->value('brokerage') ?? 0;
                self::create([
                    'member_id' => $member_id,
                    'from_id' => $from_id,
                    'type' => $type,
                    'mark' => $mark,
                    'brokerage' => $brokerage,
                    'before' => $member_brokerage,
                    'after' => bcsub($member_brokerage, $brokerage, 2),
                    'price' => $price,
                    'order_number' => $order_number
                ]);
                Member::where('id', $member_id)->dec('brokerage', $brokerage)->update();
            }
            if ($mark == '+') {
                $member_brokerage = Member::where('id', $member_id)->value('brokerage') ?? 0;//非 ?? 账户删除了 就报错了
                //            halt([
                //                'title' => $title,
                //                'member_id' => $member_id,
                //                'from_id' => $from_id,
                //                'type' => $type,
                //                'mark' => $mark,
                //                'brokerage' => $brokerage,
                //                'before' => $member_brokerage,
                //                'after' => bcadd($member_brokerage, $brokerage, 2),
                //                'price' => $price,
                //                'order_number' => $order_number
                //            ]);die;
                self::create([
                    'member_id' => $member_id,
                    'from_id' => $from_id,
                    'type' => $type,
                    'mark' => $mark,
                    'brokerage' => $brokerage,
                    'before' => $member_brokerage,
                    'after' => bcadd($member_brokerage, $brokerage, 2),
                    'price' => $price,
                    'order_number' => $order_number
                ]);
                Member::where('id', $member_id)->inc('brokerage', $brokerage)->update();
                $pre = Config::where('varname', 'web_integral')->value('varvalue');

               $integral = $brokerage*$pre/100;
               MemberIntegral::saveData($member_id,$type,$mark,$integral,$order_number);
            }



    }

    /*
     * 退回佣金和积分
     */
    public static function refund($order_number)
    {
        $member_brokerage = self::where('order_number', $order_number)->select();

        if(!empty($member_brokerage)){
            dump($member_brokerage);
            foreach ($member_brokerage as $v) {
                self::saveData($v['member_id'],$v['from_id'],'订单退款','-',$v['brokerage'],$v['price'],$v['order_number']);
            }
        }
//        $member_integral = MemberIntegral::where('content', $order_number)->select();
//        if(!empty($member_integral)){
////            dump($member_integral);
//
//            foreach ($member_integral as $vv){
//                MemberIntegral::saveData($vv['member_id'],'订单退款','-',$vv['integral'],$vv['content']);
//            }
//        }



    }

    /*
 * 退回佣金和积分
 */
    public static function back($order_number)
    {
        $member_brokerage = self::where('order_number', $order_number)->where('type','not in',['订单退款','订单恢复'])->select();

        if(!empty($member_brokerage)){
           // dump($member_brokerage);
            foreach ($member_brokerage as $v) {
                self::saveData($v['member_id'],$v['from_id'],'订单恢复','+',$v['brokerage'],$v['price'],$v['order_number']);
            }
        }
//        $member_integral = MemberIntegral::where('content', $order_number)->where('type','not in',['订单退款','订单恢复'])->select();
//
//        if(!empty($member_integral)){
//           dump($member_integral);
//            foreach ($member_integral as $vv){
//              MemberIntegral::saveData($vv['member_id'],'订单恢复','+',$vv['integral'],$vv['content']);
//            }
//        }



    }






}